package com.ckf.login_wx.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.PageHelper;
public class Jy {
  
public Object list(Page page,IService<?> service) {
    Map<String, Object> map = new HashMap<>();
    if(service.list().size()==0){
		return "没有数据";
	}
	PageHelper.offsetPage((page.getCurpage()-1)*page.getPagesize(), page.getPagesize());
	map.put("list",service.list());
    return map;
}
public Object getById(Page page,IService<?> service) {
    Map<String, Object> map = new HashMap<>();
    if(service.getById(page.getId())==null){
		return "没有数据";
	}
	PageHelper.offsetPage((page.getCurpage()-1)*page.getPagesize(), page.getPagesize());
	map.put("list",service.getById(page.getId()));
    return map;
}





//public Object save(IService<?> service,Integer max,Parent parent) {
//	Calendar Cld = Calendar.getInstance();
//	int YY = Cld.get(Calendar.YEAR) ;
//	int MM = Cld.get(Calendar.MONTH)+1;
//	String M="";
//	if(MM<10)M="0"+MM;else M=MM+"";
//	int DD = Cld.get(Calendar.DATE);
//	String D="";
//	if(DD<10)D="0"+DD;else D=DD+"";
//	
//	parent.setId(max);
//	if(parent.getTime()==null||parent.getTime().equals(""))
//		parent.setTime(YY + "-" + M + "-" + D);
//	try{
//	if(service.save(parent))
//		return "添加成功";
//	else
//		return "添加失败";
//	}catch(Exception e){
//		return "添加失败";
//	}
//}
public Object upbyid(Page page,IService<?> service) {
    Map<String, Object> map = new HashMap<>();
    if(service.getById(page.getId())==null){
		return "没有数据";
	}
	PageHelper.offsetPage((page.getCurpage()-1)*page.getPagesize(), page.getPagesize());
	map.put("list",service.getById(page.getId()));
    return map;
}
public Object dele(Page page,IService<?> service) {
    Map<String, Object> map = new HashMap<>();
    if(service.getById(page.getId())==null){
		return "没有数据";
	}
	PageHelper.offsetPage((page.getCurpage()-1)*page.getPagesize(), page.getPagesize());
	map.put("list",service.getById(page.getId()));
    return map;
}
public String gettime() {
	Calendar Cld = Calendar.getInstance();
	int YY = Cld.get(Calendar.YEAR) ;
	int MM = Cld.get(Calendar.MONTH)+1;
	String M="";
	if(MM<10)M="0"+MM;else M=MM+"";
	int DD = Cld.get(Calendar.DATE);
	String D="";
	if(DD<10)D="0"+DD;else D=DD+"";
	return YY + "-" + M + "-" + D;
}
public Object download(HttpServletResponse response,String url,String name) {
	try{
		File file = new File(url);
		// 穿件输入对象
		FileInputStream fis = new FileInputStream(file);
		// 设置相关格式
		response.setContentType("application/force-download");
		// 设置下载后的文件名以及header
		response.addHeader("Content-disposition", "attachment;fileName=" + name);
		// 创建输出对象
		OutputStream os = response.getOutputStream();
		// 常规操作
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		fis.close();
		}catch (Exception e) {
			return "下载失败";
    	} 
		return "下载成功";
}
public Object downloadexc(HttpServletResponse response,String url,String name) {
	
	try{
		File file = new File(url);
		// 穿件输入对象
		FileInputStream fis = new FileInputStream(file);
		// 设置相关格式
		response.setContentType("application/force-download");
		// 设置下载后的文件名以及header
		response.addHeader("Content-disposition", "attachment;fileName=" + name);
        //通知客服文件的MIME类型
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        //修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容？如果信任此工作簿的来源，请点击"是"
		response.setHeader("Content-Length", String.valueOf(fis.getChannel().size()));
		
		
		
		// 创建输出对象
		OutputStream os = response.getOutputStream();
		// 常规操作
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		fis.close();
		}catch (Exception e) {
			return "下载失败";
    	} 
		return "下载成功";
	
	
	
	
//	
//	try{
//		File file = new File(url);
//		// 穿件输入对象
//		FileInputStream fis = new FileInputStream(file);
//		// 设置相关格式
//
//     	String suffixName = url.substring(url.lastIndexOf("."));
//     	if(suffixName.equals(".xls")){
//    		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//     	}else{
//     		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//     	}
//		response.addHeader("Content-Disposition", "attachment;fileName=" + name);
//		// 创建输出对象
//		OutputStream os = response.getOutputStream();
//		// 常规操作
//		byte[] b = new byte[2048];
//        int len;
//        while ((len = fis.read(b)) != -1) {
//        	os.write(b, 0, len);
//        }
//        //修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容？如果信任此工作簿的来源，请点击"是"
//        response.setHeader("Content-Length", String.valueOf(fis.getChannel().size()));
//        fis.close();
//		}catch (Exception e) {
//			return "下载失败";
//    	} 
//		return "下载成功";
}
public ModelAndView ModeAndView(String url) {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.setViewName(url);
  return modelAndView;
}
public ModelAndView ModeAndView(String url,String name,Object list) {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject(name, list);
	modelAndView.setViewName(url);
  return modelAndView;
}
public ModelAndView ModeAndView(String url,String name,Object list,String keyname2,List<String> name2) {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject(name, list);
	modelAndView.addObject(keyname2, name2);
	modelAndView.setViewName(url);
  return modelAndView;
}
public ModelAndView ModeAndView(String url,String name,Object list,String keyname2,String name2) {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject(name, list);
	modelAndView.addObject(keyname2, name2);
	modelAndView.setViewName(url);
  return modelAndView;
}
public ModelAndView ModeAndView(String url,String name,Object list,String keyname2,Object name2,String keyname3,String name3) {
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject(name, list);
	modelAndView.addObject(keyname2, name2);
	modelAndView.addObject(keyname3, name3);
	modelAndView.setViewName(url);
  return modelAndView;
}
}